/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Date Modified: 18th July, 2013
 */
package ac.manchester.cs.afzal.tcm_processing.nlp;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import ac.manchester.cs.afzal.tcm_processing.miscellaneous.File_Manager_Impl;

public class Download_PMC_Documents
{

    private List<String> pmc_ids = new ArrayList<String>();
    File_Manager_Impl fileManager = new File_Manager_Impl();

    public Download_PMC_Documents()
    {
    }

    public void download_pmcids()
    {
        for (String pmcid : pmc_ids)
        {
            String BASE_URLL = "http://www.pubmedcentral.nih.gov/oai/oai.cgi?verb=GetRecord&metadataPrefix=pmc&identifier=oai:pubmedcentral.nih.gov:";
            String html_file = BASE_URLL + pmcid;

            System.out.println(html_file);
            fileManager.fileDownloader(html_file, "..\\TCM_Resources\\Data\\Corpus\\Documents\\" + pmcid + ".xml");
        }
    }

    // if PMCIDs are given in a text file as a list.
    public void download_pmcids(String input_file)
    {
        StringBuffer strBufPMCIDS = fileManager.fileReader(input_file);
        StringTokenizer st1 = new StringTokenizer(strBufPMCIDS.toString(), "\r\n");
        while (st1.hasMoreElements())
        {
            pmc_ids.add(st1.nextToken());
        }
        download_pmcids();
    }

    //Make search query and download PMCIDs into a file
    public void download_EntrezXML()
    {
        String baseURL = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=PMC&retMax=500&term=";
        String PMCIDURL = baseURL + "BMC+Complementary+and+Alternative+Medicine+AND+Chinese";
        String PMCIDFile = "..\\TCM_Resources\\Data\\Corpus\\PMCIDsDownloaded.xml";
        System.out.println(PMCIDURL);
        System.out.println(PMCIDFile);
        fileManager.fileDownloader(PMCIDURL, PMCIDFile);

    }

    public void read_search_result()
    {

        StringBuffer parContents = new StringBuffer();

        StringBuffer contents = null;
        String_Manipulator_Impl str_man = new String_Manipulator_Impl();

        contents = fileManager.fileReader("..\\TCM_Resources\\Data\\Corpus\\PMCIDsDownloaded.xml");


        String result_count = str_man.string_extractor(contents.toString(), "<Count>", "</Count>");
        System.out.println(result_count);

        int index = 0;
        String pmc_id = "";
        while ((index = contents.indexOf("<Id>", index)) != -1)
        {
            index++;
            String remaining = contents.substring(index);
            StringTokenizer st = new StringTokenizer(remaining, "</Id>");
            pmc_id = st.nextToken();
            System.out.println(pmc_id);
            //pmc_ids.add(pmc_id);
            parContents.append(pmc_id);
            parContents.append(System.getProperty("line.separator"));
            String outputFileName = "..\\TCM_Resources\\Data\\Corpus\\PMCIDs.xml";
            fileManager.fileWriter(parContents, outputFileName);
        }
    }

    public List<String> get_pmc_ids()
    {
        return pmc_ids;
    }
}
